#include "dpiservice.h"
#include "systemconfigmanager.h"
#include "processingsettings.h"
#include <QDebug>
// 静态实例初始化
DpiService* DpiService::s_instance = nullptr;
DpiService::DpiService(QObject* parent)
    : QObject(parent)
    , m_processingSettings(nullptr)
{
}
DpiService& DpiService::instance()
{
    // 懒汉式单例 - 线程安全由C++11保证
    if (!s_instance) {
        s_instance = new DpiService();
    }
    return *s_instance;
}
void DpiService::setProcessingSettings(const ProcessingSettings* settings)
{
    m_processingSettings = settings;
}
double DpiService::getDpiForPurpose(DpiPurpose purpose) const
{
    // Martin Fowler：策略模式 - 根据用途选择DPI源
    switch (purpose) {
        case DpiPurpose::CacheGeneration:
            // 缓存生成：使用系统配置
            return getSystemConfigDpi();
        case DpiPurpose::WindowRendering:
        case DpiPurpose::PdfExport:
            // 渲染和导出：使用配置面板
            return getPanelConfigDpi();
        default:
            return 200.0;
    }
}
DpiService::DpiAnalysis DpiService::analyzeDpiConfiguration() const
{
    // 获取两种DPI源
    double systemDpi = getSystemConfigDpi();
    double panelDpi = getPanelConfigDpi();
    double effectiveDpi = calculateEffectiveDpi(systemDpi, panelDpi);
    // 构建分析结果
    DpiAnalysis analysis;
    analysis.systemDpi = systemDpi;
    analysis.panelDpi = panelDpi;
    analysis.effectiveDpi = effectiveDpi;
    analysis.isCacheSufficient = (systemDpi >= panelDpi);
    // 生成建议
    if (analysis.needsWarning()) {
        analysis.suggestion = QString("建议：提高系统配置DPI到%1或以上以获得最佳质量").arg(panelDpi);
    } else if (systemDpi > panelDpi) {
        analysis.suggestion = "缓存质量高于当前需求，输出将使用较低DPI";
    } else {
        analysis.suggestion = "DPI配置匹配，输出质量最优";
    }
    // 调试输出
    return analysis;
}
bool DpiService::shouldShowWarning() const
{
    auto analysis = analyzeDpiConfiguration();
    return analysis.needsWarning();
}
QString DpiService::generateWarningMessage() const
{
    auto analysis = analyzeDpiConfiguration();
    return analysis.getWarningMessage();
}
QSize DpiService::calculateA4PixelSize(DpiPurpose purpose) const
{
    // A4尺寸：210 x 297 mm
    const double A4_WIDTH_MM = 210.0;
    const double A4_HEIGHT_MM = 297.0;
    const double MM_PER_INCH = 25.4;
    double dpi = getDpiForPurpose(purpose);
    int width = static_cast<int>(A4_WIDTH_MM / MM_PER_INCH * dpi);
    int height = static_cast<int>(A4_HEIGHT_MM / MM_PER_INCH * dpi);
    return QSize(width, height);
}
bool DpiService::validateDpiConsistency() const
{
    auto analysis = analyzeDpiConfiguration();
    bool isConsistent = (analysis.systemDpi >= analysis.panelDpi);
    return isConsistent;
}
double DpiService::getSystemConfigDpi() const
{
    // 使用SystemConfigManager获取系统配置
    const SystemConfig& config = ConfigMgr.getConfig();
    double dpi = config.getProcessingDpi();
    // Martin Fowler重构：添加调试日志确保DPI正确获取
    return dpi;
}
double DpiService::getPanelConfigDpi() const
{
    // Martin Fowler重构：依赖注入 - 使用已设置的ProcessingSettings
    if (m_processingSettings) {
        return static_cast<double>(m_processingSettings->standardDPI);
    }
    // 降级：如果ProcessingSettings未设置，使用系统配置作为默认值
    return getSystemConfigDpi();
}
double DpiService::calculateEffectiveDpi(double systemDpi, double panelDpi) const
{
    // 保护性逻辑：取较小值，避免低质量缓存被强制放大
    return qMin(systemDpi, panelDpi);
}
void DpiService::logDpiInformation(const QString& context, DpiPurpose purpose) const
{
    double dpi = getDpiForPurpose(purpose);
}